package com.zmj.sy.mom.srv.aps.utils;

import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RequiredArgsConstructor
@Component
public class SerialCodeUtils {


    private final Environment environment;

    private final StringRedisTemplate stringRedisTemplate;


    public String pickPlateKey(String type, String month) {
        String applicationName = environment.getProperty("spring.application.name");
        return applicationName + ":pick:" + type + ":" + month;
    }

    /**
     * 钢板编号
     * @return
     */
    public String pickPlate(){
        String month = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
        Long max = stringRedisTemplate.opsForValue().increment(pickPlateKey("g", month));
        return String.format("%s%s%04d", "G", LocalDate.now().format(DateTimeFormatter.ofPattern("yyMM")), max);
    }

    /**
     * 外领件编号
     * @return
     */
    public String pickWailing() {
        String month = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
        Long max = stringRedisTemplate.opsForValue().increment(pickPlateKey("w", month));
//        return String.format("%s%s%04d", "W", month, max);
        return String.format("%s%05d", LocalDate.now().format(DateTimeFormatter.ofPattern("yyMM")), max);
    }

    /**
     * 粉末编号
     * @return
     */
    public String pickPowder() {
        String month = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
        Long max = stringRedisTemplate.opsForValue().increment(pickPlateKey("f", month));
        return String.format("%s%s%04d", "F", month, max);
    }

}
